import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tlc_lock/tlc_lock.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion =
          await TlcLock.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('搜索设备'),
      ),
      backgroundColor: const Color(0xFFF8F8F8),
      body: Stack(
        children: [
          _buildContent(),
          Positioned(
              bottom: 0,
              left: 0,
              right: 0,
              child: Text(
                'Running on: $_platformVersion\n',
                textAlign: TextAlign.center,
              ))
        ],
      ),
    );
  }

  Widget _buildContent() {
    return Column(
      children: [
        Row(
          children: [
            const SizedBox(width: 10),
            Expanded(
                child: CupertinoButton(
              onPressed: () {},
              minSize: 0,
              padding: const EdgeInsets.symmetric(vertical: 10.0),
              color: CupertinoColors.activeBlue,
              child: const Text('开始扫描'),
            )),
            const SizedBox(width: 10),
            Expanded(
                child: CupertinoButton(
              onPressed: () {},
              minSize: 0,
              padding: const EdgeInsets.symmetric(vertical: 10.0),
              color: CupertinoColors.activeBlue,
              child: const Text('停止扫描'),
            )),
            const SizedBox(width: 10),
          ],
        ),
        const SizedBox(height: 10),
        Expanded(
            child: ListView.separated(
          physics: const AlwaysScrollableScrollPhysics(),
          separatorBuilder: (context, index) => Container(
            height: 1.0,
            color: CupertinoColors.systemRed,
          ),
          itemBuilder: (context, index) {
            return _buildItem();
          },
          itemCount: 3,
        ))
      ],
    );
  }

  Widget _buildItem() {
    return Container(
      height: 80,
      color: CupertinoColors.activeBlue,
    );
  }
}
